package cn.com.blueInfo.bpm.person.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.person.entity.PostDef;
import cn.com.blueInfo.bpm.person.mapper.PostDefMapper;
import cn.com.blueInfo.bpm.person.repository.PostDefRepository;
import cn.com.blueInfo.bpm.person.request.PostDefDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 岗位定义表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class PostDefRepositoryImpl
        extends ServiceImpl<PostDefMapper, PostDef>
        implements PostDefRepository, BaseRepository<PostDef, PostDefDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增岗位定义
     * @param postDef 岗位定义参数
     */
    @Override
    public Integer createPostDef(PostDef postDef) {
        return sqlExecuteResult(save(postDef));
    }

    /**
     * 批量新增岗位定义
     * @param postDefList 岗位定义参数列表
     */
    @Override
    public Integer batchCreatePostDef(List<PostDef> postDefList) {
        return sqlExecuteResult(saveBatch(postDefList), postDefList.size());
    }

    /**
     * 删除岗位定义
     * @param postDefId 岗位定义ID
     */
    @Override
    public Integer deletePostDef(Long postDefId) {
        PostDef postDef = new PostDef();
        postDef.setId(postDefId);
        postDef.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(postDef));
    }

    /**
     * 批量删除岗位定义
     * @param postDefIdList 岗位定义ID列表
     */
    @Override
    public Integer batchDeletePostDef(List<Long> postDefIdList) {
        List<PostDef> postDefList = new ArrayList<>();
        for (Long postDefId : postDefIdList) {
            PostDef postDef = new PostDef();
            postDef.setId(postDefId);
            postDef.setDeleted(BaseDeleted.DELETED);
            postDefList.add(postDef);
        }
        return sqlExecuteResult(removeBatchByIds(postDefList), postDefList.size());
    }

    /**
     * 更新部分岗位定义
     * @param postDef 岗位定义参数
     */
    @Override
    public Integer updatePostDefPartial(PostDef postDef) {
        validateUpdateId(postDef.getId());
        LambdaUpdateWrapper<PostDef> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(PostDef::getId, postDef.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部岗位定义
     * @param postDef 岗位定义参数
     */
    @Override
    public Integer updatePostDef(PostDef postDef) {
        validateUpdateId(postDef.getId());
        return sqlExecuteResult(updateById(postDef));
    }

    /**
     * 查询一条岗位定义
     * @param postDefId 岗位定义ID
     */
    @Override
    public PostDef getPostDefById(Long postDefId) {
        LambdaQueryWrapper<PostDef> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PostDef::getId, postDefId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询岗位定义
     * @param postDefQueryDTO 岗位定义参数
     */
    @Override
    public Page<PostDef> getPostDefPage(PostDefDTO.QueryDTO postDefQueryDTO) {
        Page<PostDef> postDefPage = new Page<>(
                postDefQueryDTO.getPage(), postDefQueryDTO.getSize());
        LambdaQueryWrapper<PostDef> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, postDefQueryDTO);

        BaseDeleted deleted = postDefQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(postDefPage, queryWrapper);
        } else {
            return page(postDefPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 岗位定义 数量
     * @param postDefQueryDTO 岗位定义参数
     */
    @Override
    public Long getPostDefCount(PostDefDTO.QueryDTO postDefQueryDTO) {
        LambdaQueryWrapper<PostDef> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, postDefQueryDTO);

        BaseDeleted deleted = postDefQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 岗位定义
     * @param postDefQueryDTO 岗位定义参数
     */
    @Override
    public List<PostDef> getPostDefList(PostDefDTO.QueryDTO postDefQueryDTO) {
        LambdaQueryWrapper<PostDef> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, postDefQueryDTO);

        BaseDeleted deleted = postDefQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<PostDef> queryWrapper, PostDefDTO.QueryDTO postDefQueryDTO) {

        String postCode = postDefQueryDTO.getPostCode();
        queryWrapper.eq(StringUtils.isNotBlank(postCode), PostDef::getPostCode, postCode);

        String postName = postDefQueryDTO.getPostName();
        queryWrapper.eq(StringUtils.isNotBlank(postName), PostDef::getPostName, postName);

        String postDesc = postDefQueryDTO.getPostDesc();
        queryWrapper.eq(StringUtils.isNotBlank(postDesc), PostDef::getPostDesc, postDesc);

        addQueryWrapper(queryWrapper, postDefQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<PostDef, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                PostDef::getOrderNumber,
                PostDef::getTenantId,
                PostDef::getApplicationId,
                PostDef::getStatus,
                PostDef::getDeleted,
                PostDef::getDeleteUserId,
                PostDef::getDeleteTime,
                PostDef::getRemark,
                PostDef::getCreateUserId,
                PostDef::getCreateTime,
                PostDef::getUpdateUserId,
                PostDef::getUpdateTime,
                PostDef::getExtend1,
                PostDef::getExtend2,
                PostDef::getExtend3);
    }

}
